//create by lwy by 2017年10月26日11:45:17

var page = 0;
var couponNum = 0;
$(function () {
    doCoupon();
})

function doCoupon() {
    page++;
    $.ajax({
        url:'/cardsC/searchCards.json',
        dataType:'json',
        data:{
            page:page,
            token:getItem('token'),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('#coupon_box'));
            }
        },
        success:function (data) {
            if(data.code==1){
                if (page == 1) {
                    $('#coupon_box').empty();
                    if (data.datas.data.length == 0) {
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('#coupon_box'));
                    }
                }
                var couponData = data.datas.data;
                var ifGet = '';
                for(var i=0;i<couponData.length;i++){
                    if(couponData[i].isGet==0){
                        ifGet = ''
                    }else{
                        ifGet = 'background: url(../phoneImg/coupon_bg2.jpg) no-repeat 0 0;background-size: cover;'
                    }
                    $('<div class="coupon_box mui-clearfix">\n' +
                        '        <div class="coupon-fl">\n' +
                        '            <div class="coupon_img"><img src="'+couponData[i].img+'" alt=""></div>\n' +
                        '            <div class="coupon_title"><p class="title">'+couponData[i].name+'</p><p class="money"><i>￥</i>'+couponData[i].money+'</p></div>\n' +
                        '        </div>\n' +
                        '        <div class="coupon-fr" style="'+ifGet+'">\n' +
                        '            <div class="coupon_time"><span>有效期</span><span><b>-</b>'+couponData[i].validDay+'天<b>-</b></span></div>\n' +
                        '            <div class="get" id="'+couponData[i].id+'"></div>\n' +
                        '        </div>\n' +
                        '    </div>').appendTo($('#coupon_box'));
                }
                couponNum = couponData.length;
            }
        }
    })
    return couponNum;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doCoupon();
                    if (numResult < 10) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

//立即领取
mui('body').on('tap','.get',function () {
    var id=$(this).attr('id');
    $.ajax({
        url:'/userCardsC/getCards.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            cardsId:id,
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success:function (data) {
            $('.loading2').hide();
            if(data.code==1){
                var div=document.getElementById(id);
                var par= div.parentNode;
                par.style.cssText='background: url(../phoneImg/coupon_bg2.jpg) no-repeat 0 0;background-size: cover;unbind ;'
                mui.toast('领取成功');

            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
        }
    })
})

//我的卡券
document.getElementById('myCoupon').addEventListener('tap',function () {
    window.location.href="myCoupon.html"
})
